﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADNColorPicker
{
	public partial class ColorBox : UserControl
	{
		public ColorBox()
		{
			InitializeComponent();
		}

		public event EventHandler ColorChanged;

		private void OnColorChanged()
		{
			if (ColorChanged != null) { ColorChanged(this, EventArgs.Empty); }
		}

		public Color CurrentColor	//also does not fire OnColorChanged
		{
			get { return colorStripA.BaseColor; }
			set { SetColor(value); }
		}
		//to avoid a stack overflow, none of the color controls fire a color change event when their color is set.
		private void colorWheel_ColorChanged(object sender, EventArgs e)
		{
			SetColor(colorWheel.HsvColor.ToColor());
			OnColorChanged();
		}

		private void colorStrip_ColorChanged(object sender, EventArgs e)
		{
			ColorStrip cs = (ColorStrip)sender;
			SetColor(cs.BaseColor);
			OnColorChanged();
		}

		private void hexEdit_ColorChanged(object sender, EventArgs e)
		{
			SetColor(hexEdit.Color);
			OnColorChanged();
		}

		private void SetColor(Color c)
		{
			colorWheel.HsvColor = HsvColor.FromColor(c);
			colorPreview.BackColor = c;
			colorStripA.BaseColor = c;
			colorStripR.BaseColor = c;
			colorStripG.BaseColor = c;
			colorStripB.BaseColor = c;
			if (!hexEdit.Focused) { hexEdit.Color = c; } //prevents updating if the event is coming from hexedit
		}


	}
}
